package logic

import (
	"api/model/order_food_goods"
	"api/service"
	"context"
	"github.com/zeromicro/go-zero/core/stores/sqlx"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderFoodGoodsDelLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewOrderFoodGoodsDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderFoodGoodsDelLogic {
	return &OrderFoodGoodsDelLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *OrderFoodGoodsDelLogic) OrderFoodGoodsDel(req *types.RequestOrderFoodGoodsDel) (resp *types.ResponseMsg, err error) {
	conn := sqlx.NewMysql(l.svcCtx.Config.DataSource)
	goodsModel := order_food_goods.NewOrderFoodGoodsModel(conn)

	busService := service.NewBusinessService(l.ctx, l.svcCtx)
	bus := busService.GetLoginBusinessInfo()

	err = goodsModel.Delete(l.ctx, req.Id, bus.Id)

	if err != nil {
		return service.ReturnError()
	}

	// 保存商品至redis
	redis := l.svcCtx.RedisPool.Get()
	defer redis.Close()

	goodsService := service.NewOrderFoodService(l.ctx, l.svcCtx)
	goodsService.DelGoods(redis, bus.Id, req.CategoryId, req.Id)

	return service.ReturnOk()
}
